package com.qf.fmall.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.index.entity.Product;
import com.qf.fmall.index.entity.ProductImg;
import com.qf.fmall.index.mapper.ProductImgMapper;
import com.qf.fmall.index.mapper.ProductMapper;
import com.qf.fmall.index.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall.product.entity.ProductSku;
import com.qf.fmall.product.mapper.ProductSkuMapper;
import com.qf.fmall.product.vo.ProductInfoResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author djl
 * @since 2024-04-25
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductImgMapper productImgMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Override
    public List<Product> listRecommends() {

        //1. 查询商品表，获取最新上架的三个商品
        // select * from product order by create_time desc limit 3
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time")
                .last("limit 3");
        List<Product> products = this.list(queryWrapper);

        //2. 查询商品图片表，获取商品的图片信息，别切填充到商品信息中
        for (Product product : products) {
            // select * from product_img where product_id= product.getId
            QueryWrapper<ProductImg> wrapper = new QueryWrapper<>();
            wrapper.eq("product_id",product.getProductId());
            List<ProductImg> imgs = productImgMapper.selectList(wrapper);

            product.setImgs(imgs);

        }

        return products;
    }

    @Override
    public ProductInfoResponseVo detailInfo(Integer id) {

        //1. 查询商品基本信息
        // select * from product where product_id= id
        Product product = this.getById(id);

        //2. 查询商品套餐信息
        // select * from product_sku where product_id=id
        List<ProductSku> productSkus = productSkuMapper.selectList(new QueryWrapper<ProductSku>().eq("product_id", id));

        //3. 查询商品的图片信息
        // select * from product_img where product_id=id
        List<ProductImg> imgs = productImgMapper.selectList(new QueryWrapper<ProductImg>().eq("product_id", id));

        return new ProductInfoResponseVo(product, productSkus, imgs);

    }
}
